﻿@model WidgetInstance
@{
    Layout = null;
    var tmpl = Model.WidgetDescriptor.GetTemplate();
    var _up = Model.ReadUserPreferences();
    var _json = "{}";
    if (_up != null && _up.Count > 0)
    {
        _json = (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(_up);
    }
    else
    {
        if (!string.IsNullOrEmpty(Model.WidgetDescriptor.Defaults))
        {
            _json = Model.WidgetDescriptor.Defaults;
        }
    }

    var _widgetPath = Server.MapPath("~/content/widgets/") + tmpl.ID;
    var _cssPath = _widgetPath + "\\styles";
    var _scriptPath = _widgetPath + "\\scripts";
    var _stylesheets = new List<string>();
    var _scripts = new List<string>();
    if (Directory.Exists(_cssPath))
    {
        string[] stylesheetFiles = Directory.GetFiles(_cssPath, "*.css");
        for (int i = 0; i < stylesheetFiles.Length; i++)
        {
            _stylesheets.Add("~/content/widgets/" + tmpl.ID.Replace("\\", "/") + "/styles/" + Path.GetFileName(stylesheetFiles[i]));
        }
    }

    if (Directory.Exists(_scriptPath))
    {
        string[] scriptFiles = Directory.GetFiles(_scriptPath, "*.js");
        for (int i = 0; i < scriptFiles.Length; i++)
        {
            _scripts.Add("~/content/widgets/" + tmpl.ID.Replace("\\", "/") + "/scripts/" + Path.GetFileName(scriptFiles[i]));
        }
    }
}
<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6 lte9 lte8 lte7 no-js"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7 lte9 lte8 lte7 no-js"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8 lte9 lte8 no-js"> <![endif]-->
<!--[if IE 9 ]><html class="ie ie9 lte9 no-js"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->
<html class="notie no-js">
<!--<![endif]-->
<head>
    <title>@Model.Title</title>
    @foreach (var cssStyleSheet in _stylesheets)
    { 
        <link rel="stylesheet" href="@Url.Content(cssStyleSheet)" type="text/css" />
    }
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.min.js")" ></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui.min.js")" ></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/modernizr.min.js")" ></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/dna/runtime.min.js")" ></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/dna/widgets.js")" ></script>
    @if (!string.IsNullOrEmpty(tmpl.Feature.Url))
    {
        var _path = tmpl.Feature.Url;
        if (_path.StartsWith("~/"))
        {
            _path = Url.Content(_path);
        }
        var _url = new UrlBuilder(_path);

        if (tmpl.Feature.Params != null && tmpl.Feature.Params.Count > 0)
        {
            foreach (var _param in tmpl.Feature.Params)
            {
                _url.AddParam(_param.Name, _param.Value.ToString());
            }
        }
        <script type="text/javascript" src="@_url.ToString()"></script>
    }
    <script type="text/javascript">
        $(function () {
            window.$widget = new __widget(@(MvcHtmlString.Create(_json)));
              var _pros= { 
               id:"@(Model.ID)",
               title:"@Model.Title",
               titleLinkUrl:"@Model.TitleLinkUrl",
               iconUrl:"@Model.IconUrl"
            };
            $.extend($widget,_pros);
        });
    </script>
    @foreach (var includeScript in _scripts)
    { 
        <script type="text/javascript" src="@Url.Content(includeScript)"></script>
    }
</head>
<body>
    @MvcHtmlString.Create(tmpl.Content.Text)
    <script type="text/javascript">
        function _setWinSize() {
            var _wh = $(window.document.body).height() + 10;
            $(window.frameElement).animate({ height: _wh });
        }
        $(function () {
            $(window).bind("load", function () {
                if ($(window.document.body).height()) {
                    _setWinSize();
                    $(window.document.body).css({ "overflow": "hidden" });
                }
            });
        });
    </script>
</body>
</html>
